#include <cpp_primer.h>

using namespace std;

class TextQuery {
public:
    using LineNum = vector<int>;
    using WordMessage = map<string, LineNum>;

    TextQuery(ifstream &infile)
    {
        string line;
        size_t subscript = 0;
        while(getline(infile, line)) {
            svec.push_back(line);

            istringstream iss(line);
            string word;
            while(iss >> word) {
                wordMap[word].push_back(subscript);
            }
            ++subscript;
        }
    }

    shared_ptr<map<size_t, string>> query(string &word)
    {
        auto spm = make_shared<map<size_t, string>>();
        auto it = wordMap.find(word);
        if(it != wordMap.end()) {
            for(auto i : it->second) {
                (*spm)[i] = svec[i];
            }
        }

        return spm;
    }

private:
    vector<string> svec;
    WordMessage wordMap;
};

class QueryResult{
public:
    ostream& print(ostream& os, shared_ptr<map<size_t, string>> spm)
    {
        os << spm->size() << endl;
        for(auto it : *spm) {
            os << it.first << " ";
            os << it.second << endl;
        }

        return os;
    }
};

void runQuerise(ifstream &infile)
{
    TextQuery tq(infile);
    while(true) {
        cout << "enter word to look for, or q to quit: ";
        string s;
        if(!(cin >> s) || s == "q")
            break;
        QueryResult().print(cout, tq.query(s)) << endl;
    }
}

int main()
{

    return 0;
}
